package com.xpec.canaan.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.wc.ISVNEventHandler;
import org.tmatesoft.svn.core.wc.ISVNStatusHandler;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNStatusClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class SampleTestSVN {
	
	public static void main (String[] args) {
        
        SampleTestSVN tsvn = new SampleTestSVN();
        
        File updateFile = new File(m_localPath);
        File comFile = new File(m_localPath+"//Week7//testText.txt");
        try
        {
        	RandomAccessFile nFile = new RandomAccessFile(comFile, "rw");
        	nFile.close();
        }
        catch(FileNotFoundException fnfe)
        {
        	
        }
        catch(IOException ioe)
        {
        	
        }
        
        File [] comFiles = new File[]{comFile};
        
//        tsvn.getSVNStatus(updateFile);
        
        tsvn.updateSVN(updateFile);
        tsvn.addSVN(comFile);
        tsvn.commitSVN(comFiles, "Test add Files");
        tsvn.deleteSVN(comFile);
        tsvn.commitSVN(comFiles, "Test delete Files");
	}
	
	public SampleTestSVN()
	{
		initSVN();
	}
	
	private void initSVN()
	{			
		//1. first initialize the DAV protocol
        DAVRepositoryFactory.setup();
		DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
		ourClientManager = SVNClientManager.newInstance(options, m_name, m_password);	
		
		myCommitEventHandler = new CommitEventHandler();
		myUpdateEventHandler = new UpdateEventLogHandler();
		myWCEventHandler = new WCEventHandler();
		
		ourClientManager.getCommitClient().setEventHandler(myCommitEventHandler);
		ourClientManager.getUpdateClient().setEventHandler(myUpdateEventHandler);
		ourClientManager.getWCClient().setEventHandler(myWCEventHandler);
	}
	
	public void getSVNStatus(File filePath)
	{
		ISVNStatusHandler handler = new StatusHandler(true);
		SVNStatusClient statusClient = ourClientManager.getStatusClient();
		try{
		statusClient.doStatus(filePath, SVNRevision.WORKING, SVNDepth.INFINITY, true, true, false, false, 
                handler, null);
		}
		catch(SVNException svne)
		{
			 System.out.println(svne.getMessage());
             System.exit(1);
		}
	}
	
	public void checkOutSVN(SVNURL url, File filepath)
	{
		System.out.println("check out...");
         try{
        	 ourClientManager.getUpdateClient().doCheckout(url, filepath, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, true);
         }
         catch(SVNException svne)
         {
        	 System.out.println(svne.getMessage());
             System.exit(1);
         }
	}
	
	/**
	 * update files from local
	 * @param updatePath
	 */
	public void updateSVN(File updatePath)
	{
		System.out.println("update start: " + updatePath.getAbsolutePath());
        try{
        	ourClientManager.getUpdateClient().doUpdate(updatePath, SVNRevision.HEAD,SVNDepth.INFINITY, false, true);
        }
        catch(SVNException svne)
        {
       	 	System.out.println(svne.getMessage());
            System.exit(1);
        }
	}
	
	/**
	 * add single file or directory on svn
	 * @param addPath
	 */
	public void addSVN(File addPath)
	{
		if( null == addPath )
		{
			System.err.println("add file is empty!");
			return;
		}
		
		System.out.println("adding start: " + addPath.getAbsolutePath());
		try{		
			ourClientManager.getWCClient().doAdd(addPath, false, false, false, SVNDepth.INFINITY, false, false);
		}
		catch(SVNException svne)
		{
			System.out.println(svne.getMessage());
            System.exit(1);
		}
	}
	
	/**
	 * add batch files or directories on svn
	 * @param addPath
	 */
	public void addSVN(File[] addPaths)
	{
		if( null == addPaths || 0 >= addPaths.length)
		{
			System.err.println("add file is empty!");
			return;
		}
		
		for(int i = 0; i < addPaths.length; i++)
		{
			System.out.println("adding start: " + addPaths[i].getAbsolutePath());
		}
		
		try{	
			
			ourClientManager.getWCClient().doAdd(addPaths, false, false, false, SVNDepth.INFINITY, false, false, false);
		}
		catch(SVNException svne)
		{
			System.out.println(svne.getMessage());
            System.exit(1);
		}
	}
	
	/**
	 * commit change to svn
	 * @param commitFiles
	 * @param comment
	 */
	public void commitSVN(File [] commitFiles, String comment)
	{
		if( null == commitFiles || 0 >= commitFiles.length)
		{
			System.err.println("commit file is empty!");
			return;
		}
		
		for( int i = 0; i < commitFiles.length; i++)
		{
			System.out.println("commit :" + commitFiles[i].getAbsolutePath());
		}
		
		try{					
			SVNCommitInfo info = ourClientManager.getCommitClient().doCommit(commitFiles, false, comment, null, null, false, false, SVNDepth.INFINITY);
			System.out.println(info.toString());
		}
		catch(SVNException svne)
		{
			System.out.println(svne.getMessage());
            System.exit(1);
		}
	}
	
	/**
	 * delete file from local
	 * @param deleteFile
	 */
	public void deleteSVN(File deleteFile)
	{
		if( null == deleteFile )
		{
			System.err.println("delete file is empty!");
			return;
		}
		
		System.out.println("delete file: " + deleteFile.getAbsolutePath());
		try{
			ourClientManager.getWCClient().doDelete(deleteFile, true, false);
		}
		catch(SVNException svne)
		{
			System.out.println(svne.getMessage());
            System.exit(1);
		}
	}
	
	private static final String m_localPath = "D://TestSVNFolder";
	
	private static final String m_name="shadyof@gmail.com";
	private static final String m_password="CD2yZ8gT5fF3";
	private static SVNClientManager ourClientManager;
	
	private ISVNEventHandler myUpdateEventHandler;
	private ISVNEventHandler myCommitEventHandler;
	private ISVNEventHandler myWCEventHandler;
        
}
